define(['angularAMD'], function (angularAMD) {
    angularAMD.service('A0401Service', ['$rootScope', '$filter', '$translate', '$calculater', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, $calculater, config, $MMWService, $mdDialog){
            var service = this;
            service.genController = function(options, controller){
                controller.init = function(){
                    controller.keep_plot_no = '0';
                    controller.sub_plot_method = '0';
                    controller.sub_plot_qty = 0;
                    controller.plot_info_details = [];
                    controller.plot_info = [];
                };
                controller.loadPlotList = function(){
                    $MMWService.showQueryListPNIG({
                        page_mode: 3,
                        pageRecord:50,
                        condition: [
                            {name:$filter('translate')('common.queryList.plot_no'), column_name:'LotNo', query_mode:'2', value:controller.plot_no}
                        ],
                        header: [
                            {label:$filter('translate')('common.queryList.plot_no'), code:'page_no'}
                        ],
                        setCondition:function(conditions){
                            conditions.push({column_name:'status', query_mode:'0', value:'0', merge_condition_model:'and'});
                            conditions.push({column_name:'status', query_mode:'0', value:'1', merge_condition_model:'and'});
                            conditions.push({column_name:'status', query_mode:'0', value:'2', merge_condition_model:'and'});
                            return conditions;
                        },
                        selectCode:controller.plot_no,
                        confirm : function(selectItem){
                            controller.plot_no = selectItem.page_no;
                            //controller.search();
                        }
                    });
                };
                controller.search = function(){
                    if(controller.plot_no && controller.plot_no!=''){
                        var content = {
                            module:'0',
                            plot_no:controller.plot_no,
                        };
                        subandmerge_plot_info_get(content, function(data){
                            controller.plot_info_details = data.plot_info;
                            controller.plotItem = undefined;
                            controller.keep_plot_no = '0';
                            controller.sub_plot_method = '0';
                            controller.sub_plot_qty = 0;
                            controller.plot_info = [];
                            //20180105 modify by Dustdusk for 當只有一筆的時候則自動選擇
                            if(controller.plot_info_details && controller.plot_info_details.length == 1){
                                controller.item_click(0, controller.plot_info_details[0]);
                            }
                        });
                    } else {
                        $rootScope.showAlert($filter('translate')('A0401.msg.plot_no_empty'));
                    }
                };
                controller.item_click = function(index, item){
                    if(!controller.isSelect(item)){
                        controller.plotItem = item;
                        controller.keep_plot_no = '0';
                        controller.sub_plot_method = '0';
                        controller.sub_plot_qty = 0;
                        controller.plot_info = [];
                    }
                };
                controller.isSelect = function(item){
                    if(!controller.plotItem){
                        return false;
                    } else {
                        return (controller.plotItem.plot_no == item.plot_no)
                        && (controller.plotItem.op_no == item.op_no)
                        && (controller.plotItem.status == item.status);
                    }
                };
                controller.expand = function(){
                    var msg = '';
                    if(!controller.plotItem){
                        msg = $filter('translate')('A0401.msg.plot_item_empty');
                    } else if((+controller.sub_plot_qty) <= 0){
                        msg = $filter('translate')('A0401.sub_plot_method_'+controller.sub_plot_method)+$filter('translate')('A0401.msg.sub_plot_qty_zero');
                    } else if(controller.sub_plot_method == 0){
                        if((+controller.sub_plot_qty) > (+controller.plotItem.qty)){
                            msg = $filter('translate')('A0401.msg.sub_plot_qty_greater');
                        }
                    }

                    if(msg == ''){
                        var content = {
                            keep_plot_no : controller.keep_plot_no,
                            sub_plot_method : controller.sub_plot_method,
                            sub_plot_qty : controller.sub_plot_qty,
                            plot_no : controller.plotItem.plot_no,
                            op_no : controller.plotItem.op_no,
                            status : controller.plotItem.status,
                            qty : controller.plotItem.qty
                        };
                        sub_expand_lot_process(content, function(data){
                            controller.plot_info = data.plot_info;
                        });
                    } else {
                        $rootScope.showAlert(msg);
                    }
                };
                controller.input_plot_qty = function(event, item){
                    var target = event.currentTarget;
                    angular.element(target).addClass('is-focus');
                    $calculater.open(function (num, dialog) {
                        if ((+num) <= 0) {
                            $rootScope.showAlert($filter('translate')('A0401.msg.plot_qty_zero'));
                        } else {
                            item.plot_qty = num;
                            angular.element(target).removeClass('is-focus');
                            dialog.hide();
                        }
                    },
                    function (dialog) {
                        angular.element(target).removeClass('is-focus');
                        dialog.hide();
                    },
                    {
                        defaultNum: item.plot_qty,
                        //20220819 modify by wangqq for#0117324:分批作业-子批作业开放小数功能
                        decimal: true
                    });
                };
                controller.confirm = function(callback){
                    if(controller.plot_info.length > 0){
                        var content = {
                            plot_no: controller.plotItem.plot_no,
                            plot_stamp: controller.plotItem.plot_stamp,
                            op_no: controller.plotItem.op_no,
                            status: controller.plotItem.status,
                            qty: controller.plotItem.qty,
                            plot_info:controller.plot_info,
                            userno: config.cache.account
                        };
                        sub_plot_lot_process(content, function(){
                            $rootScope.showToast($filter('translate')('A0401.msg.success'));
                            //20180509 modify by Dustdusk for M#43035: MES迭代SA處理-分批處理
                            controller.plot_info = [];
                            controller.plot_info_details = [];
                            controller.plotItem = undefined;
                            controller.keep_plot_no = '0';
                            controller.sub_plot_method = '0';
                            controller.sub_plot_qty = 0;
                            if(callback){
                                callback();
                            }
                        });
                    } else {
                        $rootScope.showAlert($filter('translate')('A0401.msg.plot_info_empty'));
                    }
                };
                return controller;
            };

            service.init = function(options){
                var content = {
                    module:'0',
                    plot_no:options.plotItem.plot_no,
                    status: options.plotItem.status,
                    op_no: options.plotItem.op_no
                };
                subandmerge_plot_info_get(content, function(data){
                    $mdDialog.dialog(
                        'module/moduleA/program/A04/A0401/A0401.tmp.html',
                        function(dialog){
                            var controller = service.genController(options, dialog);
                            controller.css_url = 'module/moduleA/program/A04/A0401/A0401.css';
                            controller.beforeShown = function(){
                                dialog.init();
                                dialog.plot_info_details = data.plot_info;
                                dialog.plotItem = dialog.plot_info_details[0];
                                dialog.plot_no = options.plotItem.plot_no;
                            };
                            controller.back = function () {
                                dialog.hide();
                            };
                            controller.dialog_confirm = function () {
                                dialog.confirm(function(){
                                    dialog.back();
                                    if(options.feedback)
                                        options.feedback();
                                });
                            };
                            return controller;
                        }
                    );
                });

            };
            function subandmerge_plot_info_get(content, callback, errorback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_WIP.subandmerge_plot_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            }

            function sub_expand_lot_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_WIP.sub_expand_lot_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            }

            function sub_plot_lot_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_WIP.sub_plot_lot_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            }
        }]);
});
